package lurker

import (
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"regexp"
	"strings"
)

func FetchData(url string) string {
	client := &http.Client{}
	request, _ := http.NewRequest("GET", url, nil)
	request.Header.Set("User-Agent", "*")
	response, error := client.Do(request)
	if error != nil {
		log.Fatal(error.Error())
		return ""
	}
	defer response.Body.Close()
	body, error := ioutil.ReadAll(response.Body)
	if error != nil {
		log.Fatal(error.Error())
		return ""
	}
	return string(body)
}

func ParseHTMLContent(htmlContent *HtmlContent) string {
	log.Println(htmlContent)
	regexContent := regexp.MustCompile(`` + htmlContent.StartTag + `(?s:(.*?))` + `` + htmlContent.EndTag + ``)
	if regexContent == nil {
		log.Fatal("regexp.MustCompile error")
		return ""
	}
	content := regexContent.FindAllStringSubmatch(htmlContent.Html, -1)
	s := ""
	for _, data := range content {
		s = data[1]
		s = strings.Replace(s, " ", "", -1)
		s = strings.Replace(s, "\t", "", -1)
		break
	}
	return s
}

func StoreToFile(fileName string, data string) {
	file, error := os.Create(fileName)
	if error != nil {
		log.Fatal(error.Error())
		return
	}
	defer file.Close()
	file.WriteString(data + "n")
}
